package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.BompUserInfoMapper;
import cc.rengu.igas.bomp.common.entity.BspsUserInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * @version 1.0
 * @date 2020.05.21
 */
public class BompUserInfoMapperImpl implements BompUserInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    @Override
    public BspsUserInfo selectBompBspsUserInfoByUserId(String instId, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompBspsUserInfoByUserId");
        BspsUserInfo userInfo = new BspsUserInfo();
        userInfo.setInstId(instId);
        userInfo.setUserId(userId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, userInfo);
        if (0 != iReturnCode) {
            rglog.error("获取机构信息失败,instId:<{}>,userId:<{}>,iReturnCode:<{}>", instId, userId, iReturnCode);
            return null;
        }
        return userInfo;
    }

    @Override
    public int updateBompBspsUserInfoByPrimaryKey(BspsUserInfo userInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompBspsUserInfoByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新用户<{}>信息表失败,iReturnCode:<{}>", userInfo.getUserId(), iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }
}
